#pragma once

#include <string>

#include "noncopyable.h"

#define LOG_INFO(log_msg_format, ...)                                                              \
    do {                                                                                           \
        Logger &logger = Logger::instance();                                                       \
        logger.set_log_level(INFO);                                                                \
        char buf[1024] = {0};                                                                      \
        snprintf(buf, 1024, log_msg_format, ##__VA_ARGS__);                                        \
        logger.log(buf);                                                                           \
    } while (0)
#define LOG_ERROR(log_msg_format, ...)                                                             \
    do {                                                                                           \
        Logger &logger = Logger::instance();                                                       \
        logger.set_log_level(ERROR);                                                               \
        char buf[1024] = {0};                                                                      \
        snprintf(buf, 1024, log_msg_format, ##__VA_ARGS__);                                        \
        logger.log(buf);                                                                           \
    } while (0)
#define LOG_FATAL(log_msg_format, ...)                                                             \
    do {                                                                                           \
        Logger &logger = Logger::instance();                                                       \
        logger.set_log_level(FATAL);                                                               \
        char buf[1024] = {0};                                                                      \
        snprintf(buf, 1024, log_msg_format, ##__VA_ARGS__);                                        \
        logger.log(buf);                                                                           \
        exit(-1);                                                                                  \
    } while (0)

#ifndef MUDEBUG
#define LOG_DEBUG(log_msg_format, ...)                                                             \
    do {                                                                                           \
        Logger &logger = Logger::instance();                                                       \
        logger.set_log_level(DEBUG);                                                               \
        char buf[1024] = {0};                                                                      \
        snprintf(buf, 1024, log_msg_format, ##__VA_ARGS__);                                        \
        logger.log(buf);                                                                           \
    } while (0)
#else
#define LOG_DEBUG(log_msg_format, ...)
#endif

// 定义日志级别
enum LogLevel {
    INFO,  // 普通信息
    ERROR, // 错误信息
    FATAL, // core信息
    DEBUG, // 调试信息
};

// 输出一个日志类
class Logger : noncopyable {
  public:
    // 获取日志唯一的实例对象
    static Logger &instance();
    // 设置日志级别
    void set_log_level(int level);
    // 写日志
    void log(std::string msg);

  private:
    int log_level_;
    Logger() {}
};
